﻿using Engine;
using Game;
//粉碎机
namespace Mekiasm
{
    public class CropCronWidget : BaseEnergyWidget
    {
        public ComponentEnergyMachine Machine;
        public CropCronWidget(ComponentEnergyMachine machine, ComponentMiner componentMiner) : base(componentMiner)
        {
            Machine = machine;
            var stackH1 = new StackPanelWidget() { Direction = LayoutDirection.Horizontal, HorizontalAlignment = WidgetAlignment.Center, VerticalAlignment = WidgetAlignment.Center };
            var stackV1 = new StackPanelWidget() { Direction = LayoutDirection.Vertical };
            var stackV2 = new StackPanelWidget() { Direction = LayoutDirection.Vertical, VerticalAlignment = WidgetAlignment.Far };
            var waterSlot = Machine.Slots[16];
            var fuelSlot = Machine.Slots[17];
            var waterSlotProgress = new FluidBaseProgress(new Vector2(48, 128), waterSlot) { Margin = new Vector2(0, 10) };
            var waterSlotWidget = new MekInventorySlotWidget() { Size = new Vector2(48f) };
            var fuelSlotWidget = new MekInventorySlotWidget() { Size = new Vector2(48f) };
            var gridPanel = new GridPanelWidget() { ColumnsCount = 4, RowsCount = 4, Margin = new Vector2(10, 0) };
            waterSlotWidget.AssignInventorySlot(machine, 16);
            waterSlotWidget.ConfigSlotMode(componentMiner);
            fuelSlotWidget.AssignInventorySlot(machine, 17);
            for (int i = 0; i < 4; i++)
            {
                for (int k = 0; k < 4; k++)
                {
                    InventorySlotWidget slot = new InventorySlotWidget() { Size = new Vector2(48f) };
                    slot.AssignInventorySlot(machine, i * 4 + k);
                    gridPanel.AddChildren(slot);
                    gridPanel.SetWidgetCell(slot, new Point2(i, k));
                }
            }
            stackV1.AddChildren(stackV2);
            stackV2.AddChildren(waterSlotProgress); stackV2.AddChildren(waterSlotWidget); stackV2.AddChildren(fuelSlotWidget);
            stackH1.AddChildren(stackV1); stackH1.AddChildren(gridPanel);
            CanvasMain.AddChildren(stackH1);
            setTitle("作物监管机");
            Tick += () => {
                waterSlotProgress.SetProgress((float)waterSlot.FuelAmount / waterSlot.FuelCapacity);
            };
        }

        public override void Update()
        {
            Tick?.Invoke();
            SetLeftBottomMoreInfo($"能量消耗:{ILibrary.FormatPowerUse(Machine.OutputPowerLimit)}\n当前储能:{ILibrary.FormatPower(Machine.Power)}\n最大储能:{ILibrary.FormatPower(Machine.MaxPower)}");
        }
    }
}
